package com.zglc.ms.shop.controller;

import static com.zglc.ms.shop.util.CommonUtils.RETURN_DATA;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zglc.ms.base.constants.ResponseCodeHelper;
import com.zglc.ms.shop.controller.log.RequestLog;
import com.zglc.ms.shop.filter.JwtHelper;
import com.zglc.ms.shop.model.Region;
import com.zglc.ms.shop.model.UserAddress;
import com.zglc.ms.shop.service.AddressService;
import com.zglc.ms.shop.service.AddressService.MenuTree;
import com.zglc.ms.shop.util.CommonUtils;

@Controller
@RequestMapping("/front/addr")
public class AddressController {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private AddressService addressService;

	private JSONArray addressJarray;
	
	@RequestMapping("/regions")
	@ResponseBody
	@RequestLog
	public Map<String, Object> regions(HttpServletRequest request){
		Map<String, Object> map = CommonUtils.successResponse();
		if(addressJarray != null && !addressJarray.isEmpty()){
			map.put("data", addressJarray);
			return map;
		}
		
		List<Region> list = addressService.getRegions();
		if (CollectionUtils.isEmpty(list)) {
			return map;
		}
		
		MenuTree tree = new MenuTree(list);
		addressJarray = tree.buildJsonTree();
		map.put("data", addressJarray);
		return map;
	}
	
	@RequestMapping("/save")
	@ResponseBody
	@RequestLog
	public Map<String, Object> save(HttpServletRequest request, @RequestParam int provId, 
			@RequestParam int cityId, @RequestParam int areaId, @RequestParam String detail, 
			@RequestParam String contact, @RequestParam String phone){
		Map<String, Object> map = CommonUtils.successResponse();
		Integer userId = JwtHelper.getUserId(request);
		if(userId == null){
			return CommonUtils.userNotExistResponse();
		}
		
		Integer id = addressService.save(userId, provId, cityId, areaId, detail, contact, phone);
		if(id != null && id > 0){
			JSONObject jo = new JSONObject();
			jo.put("addressId", id);
			map.put(RETURN_DATA, jo);
			return map;
		}
		
		map.put("code", ResponseCodeHelper.COMMON_SYSTEM_EXCEPTION);
		map.put("msg", "保存失败，请重试");
		return map;
	}
	
	@RequestMapping("/list")
	@ResponseBody
	@RequestLog
	public Map<String, Object> list(HttpServletRequest request){
		logger.info("recice list");
		Map<String, Object> map = CommonUtils.successResponse();
		Integer userId = JwtHelper.getUserId(request);
		if(userId == null){
			logger.info("recice response" + CommonUtils.userNotExistResponse().toString());
			return CommonUtils.userNotExistResponse();
		}
		
		List<UserAddress> list = addressService.getAddress(userId);
		JSONArray ja = new JSONArray();
		if(!CollectionUtils.isEmpty(list)){
			for (UserAddress userAddress: list) {
				JSONObject jo = new JSONObject();
				jo.put("id", userAddress.getId());
				jo.put("provId", userAddress.getProvId());
				jo.put("cityId", userAddress.getCityId());
				jo.put("areaId", userAddress.getAreaId());
				jo.put("detail", userAddress.getDetail());
				jo.put("phone", userAddress.getPhone());
				jo.put("contact", userAddress.getContact());
				jo.put("remark", userAddress.getRemark());
				jo.put("pca", userAddress.getPca());
				ja.add(jo);
			}
		}
		
		//ja.addAll(list);
		map.put("data", ja);
		logger.info("recice response" + map.toString());
		return map;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	@RequestLog
	public Map<String, Object> update(HttpServletRequest request, @RequestParam int id, @RequestParam int provId, 
			@RequestParam int cityId, @RequestParam int areaId, @RequestParam String detail, 
			@RequestParam String contact, @RequestParam String phone){
		Map<String, Object> map = CommonUtils.successResponse();
		Integer userId = JwtHelper.getUserId(request);
		if(userId == null){
			return CommonUtils.userNotExistResponse();
		}
		
		if(!addressService.isExistUserAddress(id, userId)){
			map.put("code", ResponseCodeHelper.USER_ADDRESS_ID_NOT_EXIST);
			map.put("msg", "用户地址不存在");
			return map;
		}
		
		Integer dbId = addressService.getAddressId(userId, provId, cityId, areaId, detail, contact, phone);
		if(dbId != null&&dbId > 0){
			if(dbId == id){
				return map;
			}else{
				map.put("code", ResponseCodeHelper.USER_ADDRESS_EXIST);
				map.put("msg", "用户地址已存在");
				return map;
			}
		}
		
		boolean isUpdate = addressService.update(id, provId, cityId, areaId, detail, contact, phone);
		if(isUpdate){
			return map;
		}
		
		map.put("code", ResponseCodeHelper.COMMON_SYSTEM_EXCEPTION);
		map.put("msg", "更新失败，请重试");
		return map;
	}
	
	@RequestMapping("/del")
	@ResponseBody
	@RequestLog
	public Map<String, Object> del(HttpServletRequest request, @RequestParam int id){
		Map<String, Object> map = CommonUtils.successResponse();
		Integer userId = JwtHelper.getUserId(request);
		if(userId == null){
			return CommonUtils.userNotExistResponse();
		}
		
		if(!addressService.isExistUserAddress(id, userId)){
			map.put("code", ResponseCodeHelper.USER_ADDRESS_ID_NOT_EXIST);
			map.put("msg", "用户地址不存在");
			return map;
		}
		
		addressService.del(id, userId);
		return map;
	}

}